/*
 * Copyright 2012-2013 EZC Group S.A. RCS Luxembourg B140949
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.yobidrive.y3.v1.client.http.handlers;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.yobidrive.y3.utils.XMLUtils;
import com.yobidrive.y3.v1.model.Flow;
import com.yobidrive.y3.v1.model.FlowAccess;
import com.yobidrive.y3.v1.model.FlowContent;
import com.yobidrive.y3.v1.model.FlowEvent;

public class FlowsUtilityParsers{

	public static Flow parseFlow(Element rootElement) {
		if ( rootElement == null ) return null;
		Flow result = new Flow();
		result.createdTs = XMLUtils.getChildText(rootElement, "createdts");
		result.systemTs = XMLUtils.getChildText(rootElement, "systemts");
		result.baseName = XMLUtils.getChildText(rootElement, "basename");
		result.managerAlias = XMLUtils.getChildText(rootElement, "manageralias");
		result.writerAlias = XMLUtils.getChildText(rootElement, "writeralias");
		result.readerAlias = XMLUtils.getChildText(rootElement, "readeralias");
		result.writeAccess = XMLUtils.stringToBoolean(XMLUtils.getChildText(rootElement, "writeaccess"),false);
		result.fullAccess = XMLUtils.stringToBoolean(XMLUtils.getChildText(rootElement, "fullaccess"),false);
		Element eventsElement = XMLUtils.getChildByName(rootElement, "flowevents");
		if ( eventsElement != null  ) {
			NodeList members = eventsElement.getElementsByTagName("flowevent");
			FlowEvent[] event = new FlowEvent[members.getLength()];
			for ( int i=0; i<members.getLength(); i++) {
				event[i] = parseFlowEvent((Element)members.item(i));
			}
			result.flowEvents = event;
		}		
		//Admin values if present
		result.dlCount = XMLUtils.stringToInt(XMLUtils.getChildText(rootElement, "dlcount"),-1);
		result.ulCount = XMLUtils.stringToInt(XMLUtils.getChildText(rootElement, "ulcount"),-1);
		result.totalSize = XMLUtils.stringToLong(XMLUtils.getChildText(rootElement, "totalsize"),-1);
		Element accessElement = XMLUtils.getChildByName(rootElement, "flowaccess");
		if ( accessElement != null ) {
			NodeList members = accessElement.getElementsByTagName("access");
			FlowAccess[] access = new FlowAccess[members.getLength()];
			for ( int i=0; i<members.getLength(); i++) {
				access[i] = parseFlowAccess((Element)members.item(i));
			}
			result.flowAccess = access;
		}
		
		return result;
	}
	
	
	public static FlowEvent parseFlowEvent(Element rootElement) {
		if ( rootElement == null ) return null;
		FlowEvent result = new FlowEvent();
		result.cid = XMLUtils.getChildText(rootElement, "cid");
		result.baseURL = XMLUtils.getChildText(rootElement, "baseurl");
		result.name = XMLUtils.getChildText(rootElement, "name");
		result.tag = XMLUtils.getChildText(rootElement, "tag");
		result.timestamp = XMLUtils.getChildText(rootElement, "timestamp");
		result.pending= XMLUtils.stringToBoolean(XMLUtils.getChildText(rootElement, "pending"),false);
		
		Element contentElement = XMLUtils.getChildByName(rootElement, "contents");
		if ( contentElement != null  ) {
			NodeList members = contentElement.getElementsByTagName("flowcontent");
			FlowContent[] content = new FlowContent[members.getLength()];
			for ( int i=0; i<members.getLength(); i++) {
				content[i] = parseFlowContent((Element)members.item(i));
			}
			result.contents = content;
		}
		return result;
	}
	
	public static FlowContent parseFlowContent(Element rootElement) {
		if ( rootElement == null ) return null;
		FlowContent result = new FlowContent();
		result.cid = XMLUtils.getChildText(rootElement, "cid");
		result.content = XMLUtils.getChildText(rootElement, "content");
		result.preview = XMLUtils.getChildText(rootElement, "preview");
		result.subject= XMLUtils.getChildText(rootElement, "subject");
		result.type = XMLUtils.getChildText(rootElement, "type");
		result.size = XMLUtils.stringToLong(XMLUtils.getChildText(rootElement, "size"),-1);
		result.dlcount = XMLUtils.stringToLong(XMLUtils.getChildText(rootElement, "dlcount"),0);
		return result;
	}

	public static FlowAccess parseFlowAccess(Element rootElement) {
		if ( rootElement == null ) return null;
		FlowAccess result = new FlowAccess();
		result.systemTs = XMLUtils.getChildText(rootElement, "systemts");
		result.userIP = XMLUtils.getChildText(rootElement, "userip");
		result.userRole = XMLUtils.getChildText(rootElement, "userrole");
		result.userUUID = XMLUtils.getChildText(rootElement, "useruuid");
		return result;
	}
	
}
